<?php
// 测试通过3001端口的登录功能

echo "=== 测试3001端口登录功能 ===\n";
$url = 'http://localhost:3001/api/auth/login';
$data = [
    'username' => 'admin',
    'password' => 'admin123'
];

// 初始化cURL
$ch = curl_init();

// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded',
    'Accept: application/json'
]);

// 执行请求
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// 输出结果
echo "请求URL: $url\n";
echo "HTTP状态码: $http_code\n";
echo "响应内容: $response\n";

// 解析响应检查是否成功
if ($http_code == 200) {
    $decoded = json_decode($response, true);
    if ($decoded && isset($decoded['token']) && isset($decoded['user'])) {
        echo "\n✅ 登录测试成功！\n";
        echo "用户名: " . $decoded['user']['username'] . "\n";
        echo "是否管理员: " . ($decoded['user']['is_admin'] ? "是" : "否") . "\n";
    } else {
        echo "\n❌ 登录响应格式不正确\n";
    }
} else {
    echo "\n❌ 登录失败\n";
}